//
//  CommonTool.swift
//  TransferNow
//
//  Created by 倪锴伦 on 2023/1/3.
//

import CoreImage
import Foundation

public class CommonTool {
    public static var appDisplayName: String {
        if let infoDictionary = Bundle.main.infoDictionary,
           let appDisplayName = infoDictionary["CFBundleDisplayName"] as? String {
            return appDisplayName
        }
        return ""
    }

    public static var majorVersion: String {
        if let infoDictionary = Bundle.main.infoDictionary,
           let majorVersion = infoDictionary["CFBundleShortVersionString"] as? String {
            return majorVersion
        }
        return ""
    }

    /// 转换中文时间到时间戳
    public static func timeStringToTimeStamp(mtime: String, mformat: String = "yyyy-MM-dd") -> Int64 {
        let format = DateFormatter()
        format.dateFormat = mformat
        if let expDate = format.date(from: mtime) {
            return Int64(expDate.timeIntervalSince1970)
        }
        return 0
    }

    /// 当前时间戳是否达到7天内
    public static func needShowExp(time: Int64) -> Bool {
        let nowTime = time - Int64(Date().timeIntervalSince1970)
        return (nowTime < (3600 * 24 * 7)) && (time != 0)
    }

    /// 高斯模糊图片
    /// - Parameters:
    ///   - image: 原始图片
    ///   - blurRadius: 范围
    /// - Returns: 图片
    public static func applyBlur(to image: UIImage, withRadius blurRadius: CGFloat) -> UIImage {
        guard let ciImage = CIImage(image: image) else { return image }

        let blurFilter = CIFilter(name: "CIBoxBlur")
        blurFilter?.setValue(ciImage, forKey: kCIInputImageKey)
        blurFilter?.setValue(blurRadius, forKey: kCIInputRadiusKey)

        guard let result = blurFilter?.value(forKey: kCIOutputImageKey) as? CIImage else { return image }

        return UIImage(ciImage: result)
    }

    /// 时间戳转string
    /// - Parameters:
    ///   - timeInterval: <#timeInterval description#>
    ///   - dateFormat: <#dateFormat description#>
    /// - Returns: <#description#>
    public static func timeIntervalChangeToTimeStr(timeInterval: Double, _ dateFormat: String? = "yyyy-MM-dd HH:mm:ss") -> String {
        let date: Date = Date(timeIntervalSince1970: timeInterval)
        let formatter = DateFormatter()
        if dateFormat == nil {
            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        } else {
            formatter.dateFormat = dateFormat
        }
        return formatter.string(from: date as Date)
    }

    /// 转化为gb，mb
    /// - Parameter size: <#size description#>
    /// - Returns: <#description#>
    public static func covertToFileString(with size: Int64, format: String = "%0.2f") -> String {
        var convertedValue: Double = Double(size)
        var multiplyFactor = 0
        let tokens = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
        while convertedValue > 1024 {
            convertedValue /= 1024
            multiplyFactor += 1
        }
        return String(format: "\(format)%@", convertedValue, tokens[multiplyFactor])
    }

    /// 转换bytes到最终展示文案
    /// - Parameter bytes: 字节数
    /// - Returns: 展示结果
    public static func converByteToFinalString(bytes: Double) -> String {
        let mbResult = converByteToMb(bytes: bytes)
        if mbResult > 1024 {
            let gbResult = converMbToGb(mbs: mbResult)
            if gbResult > 1024 {
                let tbResult = converGbToTb(gbs: gbResult)
                return String(format: "%0.2fTb", tbResult)
            } else {
                return String(format: "%0.2fGb", gbResult)
            }
        } else {
            return String(format: "%0.2fMb", mbResult)
        }
    }

    /// 转换字节为MB
    /// - Parameter bytes: 字节数
    /// - Returns: result
    public static func converByteToMb(bytes: Double) -> Double {
        let result = bytes / 1024.0 / 1024.0
        return result
    }

    /// 转换字节为GB
    /// - Parameter mbs: mb数
    /// - Returns: result
    public static func converMbToGb(mbs: Double) -> Double {
        let result = mbs / 1024.0
        return result
    }

    /// 转换字节为TB
    /// - Parameter gbs: TB数
    /// - Returns: result
    public static func converGbToTb(gbs: Double) -> Double {
        let result = gbs / 1024.0
        return result
    }
}
